package com.tingyuge.thinking.spring.dependency.injection;

import com.tingyuge.thinking.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**基于注解 constructor 方法注入
 * @author xiaodao
 * @version 1.0
 * @date 2021/6/29
 */
public class AnnotationDependencyConstructorInjectionDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注册 configuration class（配置类）
        applicationContext.register(AnnotationDependencyConstructorInjectionDemo.class);

        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);
        String xmlClassPath = "classpath:/META-INF/dependency-lookup-context.xml";
        beanDefinitionReader.loadBeanDefinitions(xmlClassPath);
        //通过BeanDefinition 注册API 方式
        //启动应用上下文
        applicationContext.refresh();
        UserHolder bean = applicationContext.getBean(UserHolder.class);
        System.out.println(bean);

        //显示关闭应用上下文
        applicationContext.close();
    }
    @Bean
    public UserHolder userHolder(User user){

        return new  UserHolder(user);
    }
}
